home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / DECOMP.C < prev    next >
Text File  |  1992-01-30  |  2KB  |  116 lines

  1. /* ------------------- decomp.c -------------------- */
  2.  
  3. /*
  4.  * Decompress the application.HLP file
  5.  * or load the application.TXT file if the .HLP file
  6.  * does not exist
  7.  */
  8.  
  9. #include "dflat.h"
  10. #include "htree.h"
  11.  
  12. static int in8;
  13. static int ct8 = 8;
  14.  
  15. static FILE *fi;
  16. static BYTECOUNTER bytectr;
  17.  
  18. static BOOL LoadingASCII;
  19.  
  20. FILE *OpenHelpFile(void)
  21. {
  22.     unsigned char c;
  23.     int freqctr;
  24.     char path[65];
  25.  
  26.     BuildFileName(path, ".hlp");
  27.  
  28.     if ((fi = fopen(path, "rb")) == NULL)    {
  29.         BuildFileName(path, ".txt");
  30.         if ((fi = fopen(path, "rt")) == NULL)
  31.             return NULL;
  32.         LoadingASCII = TRUE;
  33.     }
  34.  
  35.     if (!LoadingASCII && ht == NULL)    {
  36.         if ((ht = calloc(256, sizeof(struct htree))) != NULL)    {
  37.             /* ----- read the byte count ------ */
  38.             fread(&bytectr, sizeof bytectr, 1, fi);
  39.             /* ----- read the frequency count ------ */
  40.             fread(&freqctr, sizeof freqctr, 1, fi);
  41.             /* -------- read the characters ---------- */
  42.             while (freqctr--)   {
  43.                 fread(&c, sizeof(char), 1, fi);
  44.                 ht[c].ch = c;
  45.                 fread(&ht[c].cnt, sizeof(BYTECOUNTER), 1, fi);
  46.             }
  47.             /* ---- build the huffman tree ----- */
  48.             buildtree();
  49.         }
  50.     }
  51.     return fi;
  52. }
  53.  
  54. void *GetHelpLine(char *line)
  55. {
  56.     int h;
  57.     if (LoadingASCII)
  58.         return fgets(line, 160, fi);
  59.     *line = '\0';
  60.     while (TRUE)    {
  61.         /* ----- decompress a line from the file ------ */
  62.         h = root;
  63.         /* ----- first get a character ----- */
  64.         while (ht[h].right != -1)    {
  65.             if (ct8 == 8)   {
  66.                 if ((in8 = fgetc(fi)) == EOF)    {
  67.                     *line = '\0';
  68.                     return NULL;
  69.                 }
  70.                 ct8 = 0;
  71.             }
  72.             if (in8 & 0x80)
  73.                 h = ht[h].left;
  74.             else
  75.                 h = ht[h].right;
  76.             in8 <<= 1;
  77.             ct8++;
  78.         }
  79.         if ((*line = ht[h].ch) == '\r')
  80.             continue;
  81.         if (*line == '\n')
  82.             break;
  83.         line++;
  84.     }
  85.     *++line = '\0';
  86.     return line;
  87. }
  88.  
  89. void HelpFilePosition(long *offset, int *bit)
  90. {
  91.     *offset = ftell(fi);
  92.     if (LoadingASCII)
  93.         *bit = 0;
  94.     else    {
  95.         if (ct8 < 8)
  96.             --*offset;
  97.         *bit = ct8;
  98.     }
  99. }
  100.  
  101. void SeekHelpLine(long offset, int bit)
  102. {
  103.     int i;
  104.     fseek(fi, offset, 0);
  105.     if (!LoadingASCII)    {
  106.         ct8 = bit;
  107.         if (ct8 < 8)    {
  108.             in8 = fgetc(fi);
  109.             for (i = 0; i < bit; i++)
  110.                 in8 <<= 1;
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116.